package com.wft;

import java.util.*;

/**
 * 给定两个数组，编写一个函数来计算它们的交集
 */
public class Solution292 {

    public static void main(String[] args) {
        int[] nums1 = new int[]{1,2,2,1};
        int[] nums2 = new int[]{2,2};
        int[] re = intersection(nums1, nums2);
        System.out.println(Arrays.toString(re));
    }

    public static int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> repeatSet = new HashSet<>();
        Set<Integer> set = new HashSet<>();
        int[] max = null;
        int[] min = null;
        if(nums1.length > nums2.length){
            max = nums1;
            min = nums2;
        }else{
            max = nums2;
            min = nums1;
        }

        for(int num : max){
            set.add(num);
        }
        for(int num : min){
            if(set.contains(num)){
                repeatSet.add(num);
            }
        }

        int[] result = new int[repeatSet.size()];
        int index = 0;
        Iterator<Integer> iterator = repeatSet.iterator();
        while (iterator.hasNext()){
            result[index] = iterator.next();
            index++;
        }
        return result;
    }
}
